package csel.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;

import csel.model.*;

public class MapMaker
{
	/**
	 * @param args
	 */

	/*
	 * as of now makes this really simple and ugly map: (X is wall, o is desk,
	 * space is floor)
	 * 
	 * XXXXXXXX X X Xo oo oX Xo oX Xo oo oX Xo oX XooooooX XXXXXXXX
	 * 
	 */
	private static RealTile[][] tileMatrix;
	
	protected static Direction FLOOR_GRAY = Direction.NORTH;
	protected static Direction FLOOR_CARPET = Direction.SOUTHWEST;
	protected static Direction FLOOR_YELLOW = Direction.WEST;
	protected static Direction FLOOR_BROWN = Direction.NORTHWEST;
	protected static Direction WALL_BRICK = Direction.NORTH;
	protected static Direction WALL_WHITE = Direction.SOUTHWEST;
	
	protected static Direction DESK_TOILETDOWN = Direction.SOUTHWEST;
	protected static Direction DESK_TOILETUP = Direction.WEST;
	protected static Direction DESK_DESKDOWN = Direction.NORTH;
	protected static Direction DESK_DESKUP = Direction.NORTHWEST;
	protected static Direction DESK_DESKLEFT = Direction.NORTHEAST;
	protected static Direction DESK_DESKRIGHT = Direction.EAST;
	protected static Direction DESK_TABLE = Direction.SOUTH;

	protected static Floor makeFloor(Direction d)
	{
		Floor f = new Floor();
		f.setDirection(d);
		return f;
	}
	
	protected static Wall makeWall(Direction d)
	{
		Wall w = new Wall();
		w.setDirection(d);
		return w;
	}
	
	protected static Desk makeDesk(Direction d)
	{
		Desk desk = new Desk();
		desk.setDirection(d);
		return desk;
	}

	public static void main(String[] args)
	{
		System.out.println("Working Directory = "
				+ System.getProperty("user.dir"));

		int width = 139;
		int height = 39;
		tileMatrix = new RealTile[width][height];
		File f = new File("csebasement.txt");
		if (f.exists())
			System.out.println("I found the file!");

		Map m = new Map(tileMatrix);
		try
		{
			char oneChar;
			BufferedReader input = new BufferedReader(new FileReader(f));
			int a = 0, b = 0;
			while (((oneChar = ((char) input.read())) != '\0') && (b < height))
			{
				RealTile t = new RealTile();
				if (oneChar == 'X')
				{
					t.setTerrain(makeWall(WALL_BRICK));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("w");
					++a;
				} 
				else if (oneChar == 'Q')
				{
					t.setTerrain(makeWall(WALL_WHITE));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("Q");
					++a;
				}
				else if (oneChar == 'o')
				{
					t.setTerrain(makeDesk(DESK_TABLE));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == '{')
				{
					t.setTerrain(makeDesk(DESK_TOILETDOWN));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == '}')
				{
					t.setTerrain(makeDesk(DESK_TOILETUP));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == '[')
				{
					t.setTerrain(makeDesk(DESK_DESKDOWN));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == ']')
				{
					t.setTerrain(makeDesk(DESK_DESKUP));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == '(')
				{
					t.setTerrain(makeDesk(DESK_DESKLEFT));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == ')')
				{
					t.setTerrain(makeDesk(DESK_DESKRIGHT));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("o");
					++a;
				}
				else if (oneChar == 'p')
				{
					t.setTerrain(makeFloor(FLOOR_YELLOW));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("p");
					++a;
				} else if (oneChar == 'c')
				{
					t.setTerrain(makeFloor(FLOOR_CARPET));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print("p");
					++a;
				} else if (oneChar == ' ')
				{
					t.setTerrain(makeFloor(FLOOR_GRAY));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print(" ");
					++a;
				} else if (oneChar == 'n')
				{
					t = null;
					tileMatrix[a][b] = t;
					System.out.print("n");
					++a;
				} else if (oneChar == 'b')
				{
					t.setTerrain(makeFloor(FLOOR_BROWN));
					t.setLocation(new Location(a, b));
					t.setMap(m);
					tileMatrix[a][b] = t;
					System.out.print(" ");
					++a;
				}

				if (a >= width)
				{
					a = 0;
					++b;
					System.out.println();
				}
			}
		} catch (IOException e)
		{
			System.out.println("IO Exception!");
		}
		tileMatrix[42][15].setAreaEffect(new InstantDeath());
		tileMatrix[42][15].setDecal(new Decal("skull"));
		tileMatrix[33][1].setAreaEffect(new LevelUp());
		tileMatrix[33][1].setDecal(new Decal("star"));
		tileMatrix[38][7].setAreaEffect(new HurtPlayer(3));
		tileMatrix[38][7].setDecal(new Decal("redx"));
		tileMatrix[19][22].setAreaEffect(new HurtPlayer(3));
		tileMatrix[19][22].setDecal(new Decal("redx"));
		tileMatrix[20][22].setAreaEffect(new HurtPlayer(3));
		tileMatrix[20][22].setDecal(new Decal("redx"));
		tileMatrix[3][22].setAreaEffect(new HealDamage(3));
		tileMatrix[3][22].setDecal(new Decal("gcross"));
		tileMatrix[12][32].setAreaEffect(new Teleporter(tileMatrix[59][3]));
		tileMatrix[59][1].setAreaEffect(new Teleporter(tileMatrix[15][32]));
		tileMatrix[2][22].setAreaEffect(new Teleporter(tileMatrix[82][26]));
		Decal stairs1 = new Decal("stairs");
		stairs1.setDirection(Direction.SOUTHWEST);
		tileMatrix[2][22].setDecal(stairs1);
		tileMatrix[83][26].setAreaEffect(new Teleporter(tileMatrix[2][21]));
		Decal stairs2 = new Decal("stairs");
		stairs2.setDirection(Direction.WEST);
		tileMatrix[83][26].setDecal(stairs2);
		tileMatrix[59][1].setDecal(new Decal("warp"));

		Potion p = new Potion("mdew");
		p.setDescription("SUPER AWESOME POTION!!!");
		p.setLocation(tileMatrix[17][18]);
		tileMatrix[17][18].setItem(p);

		
		Weapon w = new Weapon(13);
		w.setDescription("Sword of a Thousand Truths");
		w.setLocation(tileMatrix[42][8]);
		tileMatrix[42][8].setItem(w);
		tileMatrix[42][8].setDecal(new Decal("puddle"));

		Armor shield = new Armor(13, "shield");
		shield.setGoesOn("arm");
		shield.setDescription("Impenetrable Backpack");
		shield.setLocation(tileMatrix[7][36]);
		tileMatrix[7][36].setItem(shield);
		
		Armor armor = new Armor(25, "armor");
		armor.setGoesOn("body");
		armor.setDescription("Green Linen Shirt");
		armor.setLocation(tileMatrix[59][2]);
		tileMatrix[59][2].setItem(armor);
		
		DamageItem d = new DamageItem(13);
		d.setLocation(tileMatrix[14][6]);
		tileMatrix[14][6].setItem(d);

		Key k = new Key();
		k.setDescription("Key to The City");
		k.setLocation(tileMatrix[7][2]);
		tileMatrix[7][2].setItem(k);

		Door door1 = new Door(k, "bigdoororange", Direction.SOUTH);
		door1.setLocation(tileMatrix[9][3]);
		tileMatrix[9][3].setItem(door1);

		Door door2 = new Door(k, "bigdoororange", Direction.WEST);
		door2.setLocation(tileMatrix[15][14]);
		tileMatrix[15][14].setItem(door2);

		Door door3 = new Door(k, "bigdoororange", Direction.EAST);
		door3.setLocation(tileMatrix[35][14]);
		tileMatrix[35][14].setItem(door3);

		Door door4 = new Door(null, "bigdoororange", Direction.SOUTH);
		door4.setLocation(tileMatrix[29][20]);
		tileMatrix[29][20].setItem(door4);

		ObstacleItem ob = new ObstacleItem("wetfloor");
		ob.setLocation(tileMatrix[13][6]);
		tileMatrix[13][6].setItem(ob);

		ObstacleItem ob1 = new ObstacleItem("wetfloor");
		ob1.setLocation(tileMatrix[42][6]);
		tileMatrix[42][6].setItem(ob1);

		ObstacleItem ob2 = new ObstacleItem("wetfloor");
		ob2.setLocation(tileMatrix[8][19]);
		tileMatrix[8][19].setItem(ob2);

		ObstacleItem ob3 = new ObstacleItem("wetfloor");
		ob3.setLocation(tileMatrix[7][25]);
		tileMatrix[7][25].setItem(ob3);

		printThingie();
		try
		{
			mapSaver(m);
		} catch (IOException e)
		{
			System.out.println("IO EXCEPTION!");
		}
	}

	public static void mapSaver(Map m) throws FileNotFoundException,
			IOException
	{
		FileOutputStream fileOut = new FileOutputStream("defaultMap");
		ObjectOutputStream serializer = new ObjectOutputStream(fileOut);

		serializer.writeObject(m);

		serializer.close();
		fileOut.close();
	}

	public static void printThingie()
	{
		int size1 = 39;
		int size2 = 139;
		for (int b = 0; b < size1; ++b)
		{
			for (int a = 0; a < size2; ++a)
			{
				if (tileMatrix[a][b] == null)
					System.out.print("n");
				else if (tileMatrix[a][b].getTerrain() instanceof Wall)
					System.out.print("X");
				else if (tileMatrix[a][b].getTerrain() instanceof Desk)
					System.out.print("o");
				else if (tileMatrix[a][b].getTerrain() instanceof Floor)
					System.out.print(" ");
				else
					System.out.print("THIS AINT RIGHT!");
			}
			System.out.println();
		}
	}

}
